package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysApp;
import com.ruoyi.system.service.ISysAppService;
import lombok.AllArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 应用信息
 */
@AllArgsConstructor
@Controller
@RequestMapping("/system/app")
public class SysAppController extends BaseController {
    private final String prefix = "system/app";

    private ISysAppService appService;


    @RequiresPermissions("system:app:view")
    @GetMapping
    public String app() {
        return prefix + "/app";
    }

    @RequiresPermissions("system:app:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysApp sysApp) {
        startPage();
        List<SysApp> list = appService.selectAppList(sysApp);
        return getDataTable(list);
    }

    /**
     * 新增应用
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存应用
     * @param sysApp 应用数据
     * @return 结果
     */
    @RequiresPermissions("system:app:add")
    @Log(title = "应用管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated SysApp sysApp) {
        if (!appService.checkAppNameUnique(sysApp)) {
            return error("新增应用'" + sysApp.getAppName() + "'失败，应用名称已存在");
        }

        if (!appService.checkClientIdUnique(sysApp)) {
            return error("新增应用'" + sysApp.getAppName() + "'失败，clientId已存在");
        }

        return toAjax(appService.insertApp(sysApp));
    }


    /**
     * 修改应用
     */
    @RequiresPermissions("system:app:edit")
    @GetMapping("/edit/{appId}")
    public String edit(@PathVariable("appId") String appId, ModelMap mmap) {
        mmap.put("app", appService.selectAppById(appId));

        return prefix + "/edit";
    }


    /**
     * 修改保存应用
     */
    @RequiresPermissions("system:app:edit")
    @Log(title = "应用管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated SysApp sysApp) {
        return toAjax(appService.updateApp(sysApp));
    }


    @RequiresPermissions("system:app:remove")
    @Log(title = "应用管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        String[] appIds = Convert.toStrArray(ids);
        if (appIds.length < 1) {
            return AjaxResult.warn("请选择需要删除的数据");
        }

        return toAjax(appService.deleteAppByIds(appIds));
    }

    /**
     * 认证状态修改
     */
    @Log(title = "应用管理", businessType = BusinessType.UPDATE)
    @RequiresPermissions("system:app:edit")
    @PostMapping("/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(SysApp sysApp) {
        return toAjax(appService.changeStatus(sysApp));
    }


    /**
     * 校验应用名称
     */
    @PostMapping("/checkAppNameUnique")
    @ResponseBody
    public boolean checkAppNameUnique(SysApp sysApp) {
        return appService.checkAppNameUnique(sysApp);
    }


    /**
     * 校验client_id
     */
    @PostMapping("/checkClientIdUnique")
    @ResponseBody
    public boolean checkClientIdUnique(SysApp sysApp) {
        return appService.checkClientIdUnique(sysApp);
    }

}